﻿using JinfuyuanProject.Entity;
using JinfuyuanProject.Entity.Filter;
using JinfuyuanProject.Repository;
using JinfuyuanProject.Repository.Interface;
using JinfuyuanProject.Service.Interface;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace JinfuyuanProject.Service
{
    public class StaffService : IStaffService
    {
        private readonly IStaffRepository _staffRepository;

        public StaffService(IStaffRepository  staffRepository)
        {
            _staffRepository = staffRepository;
        }
        #region 基础方法
        public IQueryable<Staff> GetList()
        {
            return _staffRepository.GetList();
        }
        public bool Delete(Staff staff)
        {
            return _staffRepository.Delete(staff);

        }
        public Staff GetModel(int Id)
        {
            return _staffRepository.GetModel(Id);
        }

        public int Insert(Staff staff)
        {
            return _staffRepository.Insert(staff);
        }

        public bool Update(Staff staff)
        {
            return _staffRepository.Update(staff);
        }
        #endregion

        #region 业务方法
        public List<StaffTree> GetStaffTree()
        {
            List<StaffTree> result = new List<StaffTree>();
            StaffTree stafftree = new StaffTree();
            stafftree.id = 0;
            stafftree.title = "全部";
            stafftree.children = GetAreaChildNode();
            result.Add(stafftree);
            return result;
        }
        public List<StaffTree> GetAreaChildNode()
        {
            List<StaffTree> result = new List<StaffTree>();
            if (_staffRepository.GetAreaChildNode() != null)
            {
                foreach (Role role in _staffRepository.GetAreaChildNode())
                {
                    StaffTree stafftree = new StaffTree();
                    stafftree.id = role.Id;
                    stafftree.title = role.RoleName;
                    stafftree.children = null;
                    result.Add(stafftree);
                }
                return result;
            }
            else
            {
                return null;
            }
        }
        public Hashtable StaffAPI(StaffFilter filter)
        {
            int count;
            var list = _staffRepository.StaffAPI();
            Hashtable hs = new Hashtable();
            hs["code"] = 0;
            hs["msg"] = "success";
            hs["data"] = MakeStaffList(list,filter,out count);
            hs["count"] = count;
            return hs;
        }
        public string GetNumber()
        {
            // 获取最后一个工号（处理空值）
            var lastStaff = _staffRepository.GetList().ToList().LastOrDefault();
            string newNumber = lastStaff.Numbers;
            if (lastStaff != null && !string.IsNullOrEmpty(lastStaff.Numbers))
            {
                // 使用正则拆分字母和数字部分
                var match = Regex.Match(lastStaff.Numbers, @"^(\D+)(\d+)$");
                if (match.Success)
                {
                    string prefix = match.Groups[1].Value; // 获取字母部分
                    string numStr = match.Groups[2].Value; // 获取数字部分
                                                           // 数字部分转换为整数并+1
                    if (int.TryParse(numStr, out int num))
                    {
                        num++;
                        // 保持数字位数与原始格式一致（如0001->0002）
                        newNumber = $"{prefix}{num.ToString().PadLeft(numStr.Length, '0')}";
                    }
                }
            }
            return newNumber;
        }
        //名字查询(zy)
        public Hashtable SearchStaffName(string keywords)
        {
            Hashtable hashtable = new Hashtable();
            var list = GetList().Where(x => x.Name.Contains(keywords));
            hashtable["code"] = 0;
            hashtable["msg"] = "success";
            hashtable["data"] = list;
            hashtable["count"] = list.Count();
            return hashtable;
        }
        #endregion


        #region 公共方法
        public IQueryable<StaffVM> MakeStaffList(IQueryable<StaffVM> staff,StaffFilter filter,out int count)
        {
            if (!string.IsNullOrEmpty(filter.Name))
            {
                staff= staff.Where(s => s.Name.Contains(filter.Name));
            }
            if (!string.IsNullOrEmpty(filter.Phone))
            {
                staff= staff.Where(s => s.Phone == filter.Phone);
            }
            if (filter.RoleId != 0) 
            {
                staff = staff.Where(s => s.RoleId == filter.RoleId);
            }
            if (filter.Isdelete!=null)
            {
                staff = staff.Where(s => s.Isdelete == filter.Isdelete);
            }
            count=staff.Count();
            if (filter.page!=0&&filter.limit!=0)
            {
                return staff = staff.OrderBy(x => x.Id).Skip((filter.page - 1) * filter.limit).Take(filter.limit);
            }
            else
            {
                return staff;
            }
        }
        #endregion
    }
}

